home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / tiff.c < prev    next >
C/C++ Source or Header  |  1993-05-11  |  9KB  |  317 lines

  1.  
  2.  
  3.        /*********************************************
  4.        *
  5.        *   file d:\cips\tiff.c
  6.        *
  7.        *   Functions: This file contains
  8.        *     read_tiff_header
  9.        *     extract_long_from_buffer
  10.        *     extract_short_from_buffer
  11.        *
  12.        *   Purpose:
  13.        *     This file contains the subroutines that 
  14.        *     read the tiff files header information.
  15.        *
  16.        *   External Calls:
  17.        *      none
  18.        *
  19.        *   Modifications:
  20.        *      23 June 1990 - created
  21.        *      28 March 1993 - using fopen, fread, fseek
  22.        *          instead of my_open, my_read, lseek.
  23.        *
  24.        ************************************************/
  25.  
  26. #include "cips.h"
  27.  
  28.  
  29.        /***********************************************
  30.        *
  31.        *   read_tiff_header(...
  32.        *
  33.        *   This function reads the header of a TIFF 
  34.        *   file and places the needed information into
  35.        *   the struct tiff_header_struct.
  36.        *
  37.        ***********************************************/
  38.  
  39. read_tiff_header(file_name, image_header)
  40.    char file_name[];
  41.    struct tiff_header_struct *image_header;
  42. {
  43.    char buffer[12], response[80];
  44.  
  45.    FILE *image_file;
  46.  
  47.    int  bytes_read,
  48.         closed,
  49.         i,
  50.         j,
  51.         lsb,
  52.         not_finished,
  53.         position;
  54.  
  55.    long bits_per_pixel,
  56.         image_length,
  57.         image_width,
  58.         length_of_field,
  59.         offset_to_ifd,
  60.         strip_offset,
  61.         subfile,
  62.         value;
  63.  
  64.    short entry_count,
  65.          field_type,
  66.          s_bits_per_pixel,
  67.          s_image_length,
  68.          s_image_width,
  69.          s_strip_offset,
  70.          tag_type;
  71.  
  72.    image_file = fopen(file_name, "rb");
  73.    if(image_file != NULL){
  74.  
  75.         /*************************************
  76.         *
  77.         *   Determine if the file uses MSB
  78.         *   first or LSB first
  79.         *
  80.         *************************************/
  81.  
  82.    bytes_read = fread(buffer, 1, 8, image_file);
  83.  
  84.    if(buffer[0] == 0x49)
  85.       lsb = 1;
  86.    else
  87.       lsb = 0;
  88.  
  89.         /*************************************
  90.         *
  91.         *   Read the offset to the IFD
  92.         *
  93.         *************************************/
  94.  
  95.    extract_long_from_buffer(buffer, lsb, 4, 
  96.                             &offset_to_ifd);
  97.  
  98.    not_finished = 1;
  99.    while(not_finished){
  100.  
  101.         /*************************************
  102.         *
  103.         *   Seek to the IFD and read the
  104.         *   entry_count, i.e. the number of
  105.         *   entries in the IFD.
  106.         *
  107.         *************************************/
  108.  
  109.       position   = fseek(image_file, offset_to_ifd, 
  110.                          SEEK_SET);
  111.       bytes_read = fread(buffer, 1, 2, image_file);
  112.       extract_short_from_buffer(buffer, lsb, 0, 
  113.                                 &entry_count);
  114.  
  115.         /***************************************
  116.         *
  117.         *   Now loop over the directory entries.
  118.         *   Look only for the tags we need.  These
  119.         *   are:
  120.         *     ImageLength
  121.         *     ImageWidth
  122.         *     BitsPerPixel(BitsPerSample)
  123.         *     StripOffset
  124.         *
  125.         *****************************************/
  126.  
  127.       for(i=0; i<entry_count; i++){
  128.        bytes_read = fread(buffer, 1, 12, image_file);
  129.        extract_short_from_buffer(buffer, lsb, 0, 
  130.                                  &tag_type);
  131.  
  132.        switch(tag_type){
  133.  
  134.           case 255: /* Subfile Type */
  135.              extract_short_from_buffer(buffer, lsb, 2,
  136.                                        &field_type);
  137.              extract_short_from_buffer(buffer, lsb, 4,
  138.                                     &length_of_field);
  139.              extract_long_from_buffer(buffer, lsb, 8, 
  140.                                       &subfile);
  141.              break;
  142.  
  143.           case 256: /* ImageWidth */
  144.              extract_short_from_buffer(buffer, lsb, 2, 
  145.                                        &field_type);
  146.              extract_short_from_buffer(buffer, lsb, 4,
  147.                                     &length_of_field);
  148.              if(field_type == 3){
  149.               extract_short_from_buffer(buffer, lsb, 8,
  150.                                      &s_image_width);
  151.               image_width = s_image_width;
  152.              }
  153.              else
  154.               extract_long_from_buffer(buffer, lsb, 8, 
  155.                                        &image_width);
  156.              break;
  157.  
  158.           case 257: /* ImageLength */
  159.              extract_short_from_buffer(buffer, lsb, 2, 
  160.                                        &field_type);
  161.              extract_short_from_buffer(buffer, lsb, 4,
  162.                                     &length_of_field);
  163.              if(field_type == 3){
  164.               extract_short_from_buffer(buffer, lsb, 8,
  165.                                     &s_image_length);
  166.               image_length = s_image_length;
  167.              }
  168.              else
  169.               extract_long_from_buffer(buffer, lsb, 8,
  170.                                        &image_length);
  171.              break;
  172.  
  173.           case 258: /* BitsPerSample */
  174.              extract_short_from_buffer(buffer, lsb, 2, 
  175.                                        &field_type);
  176.              extract_short_from_buffer(buffer, lsb, 4,
  177.                                     &length_of_field);
  178.              if(field_type == 3){
  179.               extract_short_from_buffer(buffer, lsb, 8,
  180.                                    &s_bits_per_pixel);
  181.               bits_per_pixel = s_bits_per_pixel;
  182.              }
  183.              else
  184.               extract_long_from_buffer(buffer, lsb, 8,
  185.                                     &bits_per_pixel);
  186.              break;
  187.  
  188.           case 273: /* StripOffset */
  189.              extract_short_from_buffer(buffer, lsb, 2, 
  190.                                        &field_type);
  191.              extract_short_from_buffer(buffer, lsb, 4,
  192.                                     &length_of_field);
  193.              if(field_type == 3){
  194.               extract_short_from_buffer(buffer, lsb, 8,
  195.                                     &s_strip_offset);
  196.               strip_offset = s_strip_offset;
  197.              }
  198.              else
  199.               extract_long_from_buffer(buffer, lsb, 8,
  200.                                        &strip_offset);
  201.              break;
  202.  
  203.           default:
  204.              break;
  205.  
  206.        }  /* ends switch tag_type */
  207.  
  208.       }  /* ends loop over i directory entries */
  209.  
  210.       bytes_read = fread(buffer, 1, 4, image_file);
  211.       extract_long_from_buffer(buffer, lsb, 0, 
  212.                                &offset_to_ifd);
  213.       if(offset_to_ifd == 0) not_finished = 0;
  214.  
  215.    }  /* ends while not_finished */
  216.  
  217.  
  218.    image_header->lsb                = lsb;
  219.    image_header->bits_per_pixel = bits_per_pixel;
  220.    image_header->image_length       = image_length;
  221.    image_header->image_width        = image_width;
  222.    image_header->strip_offset       = strip_offset;
  223.  
  224.    closed = fclose(image_file);
  225.    }  /* ends if file opened ok */
  226.    else{
  227.       printf("\n\nTIFF.C> ERROR - could not open "
  228.              "tiff file");
  229.    }
  230. }  /* ends read_tiff_header */
  231.  
  232.  
  233.  
  234.  
  235.  
  236.    /****************************************
  237.    *
  238.    *   extract_long_from_buffer(...
  239.    *
  240.    *   This takes a four byte long out of a
  241.    *   buffer of characters.
  242.    *
  243.    *   It is important to know the byte order
  244.    *   LSB or MSB.
  245.    *
  246.    ****************************************/
  247.  
  248.  
  249. extract_long_from_buffer(buffer, lsb, start, number)
  250.    char  buffer[];
  251.    int       lsb, start;
  252.    long  *number;
  253. {
  254.    int i;
  255.    union long_char_union lcu;
  256.  
  257.    if(lsb == 1){
  258.       lcu.l_alpha[0] = buffer[start+0];
  259.       lcu.l_alpha[1] = buffer[start+1];
  260.       lcu.l_alpha[2] = buffer[start+2];
  261.       lcu.l_alpha[3] = buffer[start+3];
  262.    }  /* ends if lsb = 1 */
  263.  
  264.    if(lsb == 0){
  265.       lcu.l_alpha[0] = buffer[start+3];
  266.       lcu.l_alpha[1] = buffer[start+2];
  267.       lcu.l_alpha[2] = buffer[start+1];
  268.       lcu.l_alpha[3] = buffer[start+0];
  269.    }  /* ends if lsb = 0      */
  270.  
  271.    *number = lcu.l_num;
  272.  
  273.  
  274. }  /* ends extract_long_from_buffer */
  275.  
  276.  
  277.  
  278.  
  279.  
  280.